# -*-coding:utf-8 -*-
"""
# File       : main_gui.py
# Time       ：2022/3/21 11:05
# Author     ：Coco
# version    ：python 3.7
"""
import sys

from PyQt5.QtCore import QSize
from PyQt5.QtGui import QIcon
from PyQt5.QtWidgets import QApplication, QMainWindow, QWidget, QPushButton, QLabel, QVBoxLayout, QHBoxLayout, \
    QStatusBar, QToolButton, QToolBar, QGroupBox, QFormLayout, QLineEdit, QGridLayout, QComboBox, QListWidget, \
    QListWidgetItem, QListView

OPEN_ICON_PATH = "./static/style_sheet/icons/open.png"
LE_LENGTH = 120
UNIT_LENGTH = 70


class Gui(QMainWindow):
    def __init__(self):
        super(Gui, self).__init__()
        # 读取 QSS 并设置
        with open("./static/style_sheet/origin.qss", mode='r', encoding='utf-8') as f:
            style_sheet = f.read()
        self.setStyleSheet(style_sheet)
        # 窗口标题、Logo
        self.setWindowIcon(QIcon("./static/style_sheet/icons/logo.png"))
        self.setWindowTitle("  宏芯软件")
        self._setup_ui()
        self.resize(1280, 880)

    def _setup_ui(self):
        # 工具栏
        self.tool_bar = QToolBar()
        self.tb_start = QToolButton()
        self.tb_start.setIcon(QIcon("./static/style_sheet/icons/start.png"))
        self.tb_pause = QToolButton()
        self.tb_pause.setIcon(QIcon("./static/style_sheet/icons/pause.png"))
        self.tb_continue = QToolButton()
        self.tb_continue.setIcon(QIcon("./static/style_sheet/icons/continue.png"))
        self.tb_stop = QToolButton()
        self.tb_stop.setIcon(QIcon("./static/style_sheet/icons/stop.png"))
        self.tb_save_config = QToolButton()
        self.tb_save_config.setIcon(QIcon("./static/style_sheet/icons/save_file.png"))
        self.tb_read_config = QToolButton()
        self.tb_read_config.setIcon(QIcon("./static/style_sheet/icons/open_file.png"))

        self.tb_pair_smu_dcp = QToolButton()
        self.tb_pair_smu_dcp.setIcon(QIcon("./static/style_sheet/icons/setting.png"))

        self.tb_import_subdie_file = QToolButton()
        self.tb_import_subdie_file.setIcon(QIcon("./static/style_sheet/icons/import.png"))

        self.tool_bar.addWidget(self.tb_start)
        self.tool_bar.addWidget(self.tb_pause)
        self.tool_bar.addWidget(self.tb_continue)
        self.tool_bar.addWidget(self.tb_stop)
        self.tool_bar.addWidget(self.tb_save_config)
        self.tool_bar.addWidget(self.tb_read_config)
        self.tool_bar.addWidget(self.tb_pair_smu_dcp)
        self.tool_bar.addWidget(self.tb_import_subdie_file)
        self.tool_bar.setMovable(False)
        self.addToolBar(self.tool_bar)

        # 状态栏
        self.statusbar = QStatusBar()
        self.setStatusBar(self.statusbar)
        # 红色状态标签
        self.lb_status_red = QLabel("仪表连接状态: ")
        self.lb_status_red.setObjectName("lb_status_red")
        # 绿色状态标签
        self.lb_status_green = QLabel("仪表连接状态: ")
        self.lb_status_green.setObjectName("lb_status_green")

        self.statusbar.addWidget(self.lb_status_red)
        self.statusbar.addWidget(self.lb_status_green)
        self.lb_status_green.hide()

        # 主界面控件
        self.wd_main = QWidget()
        self.vl_main = QVBoxLayout(self.wd_main)

        # 各个部分
        # 测试信息输入部分
        self.wd_top = QWidget()
        self.hl_top = QHBoxLayout(self.wd_top)
        self.hl_top.setContentsMargins(0, 0, 0, 0)
        self.hl_top.setSpacing(0)
        self.gb_test_info = QGroupBox("Test InFo")
        self.fl_test_info = QFormLayout(self.gb_test_info)
        self.fl_test_info.setVerticalSpacing(5)
        self.fl_test_info.setHorizontalSpacing(20)
        self.lb_operator = QLabel("Operator:")
        self.le_operator = QLineEdit()
        self.le_operator.setClearButtonEnabled(True)
        self.lb_data = QLabel("Data:")
        self.le_data = QLineEdit()
        self.le_data.setClearButtonEnabled(True)
        self.lb_batch_num = QLabel("Batch Number:")
        self.le_batch_num = QLineEdit()
        self.le_batch_num.setClearButtonEnabled(True)
        self.lb_lot_id = QLabel("Lot ID:")
        self.le_lot_id = QLineEdit()
        self.le_lot_id.setClearButtonEnabled(True)
        self.lb_wafer_id = QLabel("Wafer ID:")
        self.le_wafer_id = QLineEdit()
        self.le_wafer_id.setClearButtonEnabled(True)
        self.le_operator.setFixedWidth(250)
        self.le_data.setFixedWidth(250)
        self.le_batch_num.setFixedWidth(250)
        self.le_lot_id.setFixedWidth(250)
        self.le_wafer_id.setFixedWidth(250)
        self.fl_test_info.addRow(self.lb_operator, self.le_operator)
        self.fl_test_info.addRow(self.lb_data, self.le_data)
        self.fl_test_info.addRow(self.lb_batch_num, self.le_batch_num)
        self.fl_test_info.addRow(self.lb_lot_id, self.le_lot_id)
        self.fl_test_info.addRow(self.lb_wafer_id, self.le_wafer_id)

        # 测试结果部分
        self.gb_test_result = QGroupBox("Test Result")
        self.fl_test_result = QFormLayout(self.gb_test_result)
        self.fl_test_result.setVerticalSpacing(10)
        self.fl_test_result.setHorizontalSpacing(20)
        self.lb_spp_proj = QLabel("SPP 工程路径：")
        self.wd_spp_proj = QWidget()
        self.hl_spp_proj = QHBoxLayout(self.wd_spp_proj)
        self.hl_spp_proj.setContentsMargins(0, 0, 0, 0)
        self.le_spp_proj = QLineEdit()
        self.pb_spp_proj = QPushButton()
        self.pb_spp_proj.setMaximumWidth(50)
        self.pb_spp_proj.setIcon(QIcon(OPEN_ICON_PATH))
        self.hl_spp_proj.addWidget(self.le_spp_proj)
        self.hl_spp_proj.addWidget(self.pb_spp_proj)
        self.hl_spp_proj.addStretch(1)
        self.lb_map_path = QLabel("Map 保存路径：")
        self.wd_map_path = QWidget()
        self.hl_map_path = QHBoxLayout(self.wd_map_path)
        self.hl_map_path.setContentsMargins(0, 0, 0, 0)
        self.le_map_path = QLineEdit()
        self.pb_map_path = QPushButton()
        self.pb_map_path.setMaximumWidth(50)
        self.pb_map_path.setIcon(QIcon(OPEN_ICON_PATH))
        self.hl_map_path.addWidget(self.le_map_path)
        self.hl_map_path.addWidget(self.pb_map_path)
        self.hl_map_path.addStretch(1)

        self.lb_file_folder = QLabel("存储文件夹：")
        self.wd_file_folder = QWidget()
        self.hl_file_folder = QHBoxLayout(self.wd_file_folder)
        self.hl_file_folder.setContentsMargins(0, 0, 0, 0)
        self.le_file_folder = QLineEdit()
        self.pb_file_folder = QPushButton()
        self.pb_file_folder.setMaximumWidth(50)
        self.pb_file_folder.setIcon(QIcon(OPEN_ICON_PATH))
        self.hl_file_folder.addWidget(self.le_file_folder)
        self.hl_file_folder.addWidget(self.pb_file_folder)
        self.hl_file_folder.addStretch(1)

        self.lb_test_batch = QLabel("测试批次号：")
        self.le_test_batch = QLineEdit()
        self.le_test_batch.setClearButtonEnabled(True)
        self.le_spp_proj.setEnabled(False)
        self.le_map_path.setEnabled(False)
        self.le_file_folder.setEnabled(False)
        self.le_spp_proj.setFixedWidth(300)
        self.le_map_path.setFixedWidth(300)
        self.le_file_folder.setFixedWidth(300)
        self.le_test_batch.setFixedWidth(300)
        self.fl_test_result.addRow(self.lb_spp_proj, self.wd_spp_proj)
        self.fl_test_result.addRow(self.lb_map_path, self.wd_map_path)
        self.fl_test_result.addRow(self.lb_file_folder, self.wd_file_folder)
        self.fl_test_result.addRow(self.lb_test_batch, self.le_test_batch)
        self.hl_top.addWidget(self.gb_test_info)
        self.hl_top.addWidget(self.gb_test_result)

        # 全局设置部分
        self.gb_global_settings = QGroupBox("Global Settings")
        self.hl_global_settings = QHBoxLayout(self.gb_global_settings)

        # TE模对准  O-O
        self.gb_oo_settings = QGroupBox("TE模对准O-O设置")
        self.gl_oo_settings = QGridLayout(self.gb_oo_settings)
        self.gb_oo_settings.setContentsMargins(0, 0, 0, 0)

        self.lb_oo_8164 = QLabel("8164B:")
        self.wd_oo_8164_power = QWidget()
        self.hl_oo_8164_power = QHBoxLayout(self.wd_oo_8164_power)
        self.hl_oo_8164_power.setContentsMargins(0, 0, 0, 0)
        self.lb_oo_8164_power = QLabel("功率设置：")
        self.le_oo_8164_power = QLineEdit()
        self.le_oo_8164_power.setClearButtonEnabled(True)
        self.cb_oo_8164_power = QComboBox()
        self.cb_oo_8164_power.addItems(['dBm'])
        self.hl_oo_8164_power.addWidget(self.lb_oo_8164_power)
        self.hl_oo_8164_power.addWidget(self.le_oo_8164_power)
        self.hl_oo_8164_power.addWidget(self.cb_oo_8164_power)
        self.hl_oo_8164_power.addStretch(1)

        self.wd_oo_8164_wave = QWidget()
        self.hl_oo_8164_wave = QHBoxLayout(self.wd_oo_8164_wave)
        self.hl_oo_8164_wave.setContentsMargins(0, 0, 0, 0)
        self.lb_oo_8164_wave = QLabel("波长设置：")
        self.le_oo_8164_wave = QLineEdit()
        self.le_oo_8164_wave.setClearButtonEnabled(True)
        self.cb_oo_8164_wave = QComboBox()
        self.cb_oo_8164_wave.addItems(['nm'])
        self.hl_oo_8164_wave.addWidget(self.lb_oo_8164_wave)
        self.hl_oo_8164_wave.addWidget(self.le_oo_8164_wave)
        self.hl_oo_8164_wave.addWidget(self.cb_oo_8164_wave)
        self.hl_oo_8164_wave.addStretch(1)

        self.lb_oo_7744 = QLabel("7744C:")
        self.wd_oo_7744_range = QWidget()
        self.hl_oo_7744_range = QHBoxLayout(self.wd_oo_7744_range)
        self.hl_oo_7744_range.setContentsMargins(0, 0, 0, 0)
        self.lb_oo_7744_range = QLabel("量程设置：")
        self.cb_oo_7744_range = QComboBox()
        self.cb_oo_7744_range.addItems(['1', '2', '3'])
        self.hl_oo_7744_range.addWidget(self.lb_oo_7744_range)
        self.hl_oo_7744_range.addWidget(self.cb_oo_7744_range)
        self.hl_oo_7744_range.addStretch(1)

        self.gl_oo_settings.addWidget(self.lb_oo_8164, 0, 0)
        self.gl_oo_settings.addWidget(self.wd_oo_8164_power, 0, 1)
        self.gl_oo_settings.addWidget(self.wd_oo_8164_wave, 1, 1)
        self.gl_oo_settings.addWidget(self.lb_oo_7744, 4, 0)
        self.gl_oo_settings.addWidget(self.wd_oo_7744_range, 4, 1)

        # TE模对准  O-E
        self.gb_oe_settings = QGroupBox("TE模对准O-E设置")
        self.gl_oe_settings = QGridLayout(self.gb_oe_settings)
        self.gb_oe_settings.setContentsMargins(0, 0, 0, 0)

        self.lb_oe_8164 = QLabel("8164B:")
        self.wd_oe_8164_power = QWidget()
        self.hl_oe_8164_power = QHBoxLayout(self.wd_oe_8164_power)
        self.hl_oe_8164_power.setContentsMargins(0, 0, 0, 0)
        self.lb_oe_8164_power = QLabel("功率设置：")
        self.le_oe_8164_power = QLineEdit()
        self.le_oe_8164_power.setClearButtonEnabled(True)
        self.cb_oe_8164_power = QComboBox()
        self.cb_oe_8164_power.addItems(['dBm'])
        self.hl_oe_8164_power.addWidget(self.lb_oe_8164_power)
        self.hl_oe_8164_power.addWidget(self.le_oe_8164_power)
        self.hl_oe_8164_power.addWidget(self.cb_oe_8164_power)
        self.hl_oe_8164_power.addStretch(1)

        self.wd_oe_8164_wave = QWidget()
        self.hl_oe_8164_wave = QHBoxLayout(self.wd_oe_8164_wave)
        self.hl_oe_8164_wave.setContentsMargins(0, 0, 0, 0)
        self.lb_oe_8164_wave = QLabel("波长设置：")
        self.le_oe_8164_wave = QLineEdit()
        self.le_oe_8164_wave.setClearButtonEnabled(True)
        self.cb_oe_8164_wave = QComboBox()
        self.cb_oe_8164_wave.addItems(['nm'])
        self.hl_oe_8164_wave.addWidget(self.lb_oe_8164_wave)
        self.hl_oe_8164_wave.addWidget(self.le_oe_8164_wave)
        self.hl_oe_8164_wave.addWidget(self.cb_oe_8164_wave)
        self.hl_oe_8164_wave.addStretch(1)

        self.lb_oe_source = QLabel("源表：")
        self.wd_oe_source_offset = QWidget()
        self.hl_oe_source_offset = QHBoxLayout(self.wd_oe_source_offset)
        self.hl_oe_source_offset.setContentsMargins(0, 0, 0, 0)
        self.lb_oe_source_offset = QLabel("偏置电压：")
        self.le_oe_source_offset = QLineEdit()
        self.le_oe_source_offset.setClearButtonEnabled(True)
        self.cb_oe_source_offset = QComboBox()
        self.cb_oe_source_offset.addItems(["mV", "V"])
        self.hl_oe_source_offset.addWidget(self.lb_oe_source_offset)
        self.hl_oe_source_offset.addWidget(self.le_oe_source_offset)
        self.hl_oe_source_offset.addWidget(self.cb_oe_source_offset)
        self.hl_oe_source_offset.addStretch(1)

        self.wd_oe_source_range = QWidget()
        self.hl_oe_source_range = QHBoxLayout(self.wd_oe_source_range)
        self.hl_oe_source_range.setContentsMargins(0, 0, 0, 0)

        self.lb_oe_source_range = QLabel("量程设置：")
        self.cb_oe_source_range = QComboBox()
        self.cb_oe_source_range.addItems(["Auto"])
        self.cb_oe_source_range.setEnabled(False)
        self.hl_oe_source_range.addWidget(self.lb_oe_source_range)
        self.hl_oe_source_range.addWidget(self.cb_oe_source_range)
        self.hl_oe_source_range.addStretch(1)

        self.wd_oe_source_speed = QWidget()
        self.hl_oe_source_speed = QHBoxLayout(self.wd_oe_source_speed)
        self.hl_oe_source_speed.setContentsMargins(0, 0, 0, 0)
        self.lb_oe_source_speed = QLabel("积分时间：")
        self.cb_oe_source_speed = QComboBox()
        self.cb_oe_source_speed.addItems(["1PLC"])
        self.cb_oe_source_speed.setEnabled(False)
        self.hl_oe_source_speed.addWidget(self.lb_oe_source_speed)
        self.hl_oe_source_speed.addWidget(self.cb_oe_source_speed)
        self.hl_oe_source_speed.addStretch(1)

        self.gl_oe_settings.addWidget(self.lb_oe_8164, 0, 0)
        self.gl_oe_settings.addWidget(self.wd_oe_8164_power, 0, 1)
        self.gl_oe_settings.addWidget(self.wd_oe_8164_wave, 1, 1)
        self.gl_oe_settings.addWidget(self.lb_oe_source, 4, 0)
        self.gl_oe_settings.addWidget(self.wd_oe_source_offset, 4, 1)
        self.gl_oe_settings.addWidget(self.wd_oe_source_range, 5, 1)
        self.gl_oe_settings.addWidget(self.wd_oe_source_speed, 6, 1)

        # 链路损耗校准
        self.gb_lc_settings = QGroupBox("链路损耗校准设置：")
        self.gl_lc_settings = QGridLayout(self.gb_lc_settings)
        self.gb_lc_settings.setContentsMargins(0, 0, 0, 0)

        self.lb_lc_8164 = QLabel("8164B:")
        self.wd_lc_8164_power = QWidget()
        self.hl_lc_8164_power = QHBoxLayout(self.wd_lc_8164_power)
        self.hl_lc_8164_power.setContentsMargins(0, 0, 0, 0)
        self.lb_lc_8164_power = QLabel("功率设置：")
        self.le_lc_8164_power = QLineEdit()
        self.le_lc_8164_power.setClearButtonEnabled(True)
        self.cb_lc_8164_power = QComboBox()
        self.cb_lc_8164_power.addItems(['dBm'])
        self.hl_lc_8164_power.addWidget(self.lb_lc_8164_power)
        self.hl_lc_8164_power.addWidget(self.le_lc_8164_power)
        self.hl_lc_8164_power.addWidget(self.cb_lc_8164_power)
        self.hl_lc_8164_power.addStretch(1)

        self.wd_lc_8164_step = QWidget()
        self.hl_lc_8164_step = QHBoxLayout(self.wd_lc_8164_step)
        self.hl_lc_8164_step.setContentsMargins(0, 0, 0, 0)
        self.lb_lc_8164_step = QLabel("步进设置：")
        self.le_lc_8164_step = QLineEdit()
        self.le_lc_8164_step.setClearButtonEnabled(True)
        self.cb_lc_8164_step = QComboBox()
        self.cb_lc_8164_step.addItems(['nm'])
        self.hl_lc_8164_step.addWidget(self.lb_lc_8164_step)
        self.hl_lc_8164_step.addWidget(self.le_lc_8164_step)
        self.hl_lc_8164_step.addWidget(self.cb_lc_8164_step)
        self.hl_lc_8164_step.addStretch(1)

        self.wd_lc_8164_wave = QWidget()
        self.hl_lc_8164_wave = QHBoxLayout(self.wd_lc_8164_wave)
        self.hl_lc_8164_wave.setContentsMargins(0, 0, 0, 0)
        self.lb_lc_8164_wave = QLabel("波长设置：")
        self.le_lc_8164_wave_start = QLineEdit()
        self.le_lc_8164_wave_start.setClearButtonEnabled(True)
        self.le_lc_8164_wave_stop = QLineEdit()
        self.le_lc_8164_wave_stop.setClearButtonEnabled(True)
        self.cb_lc_8164_wave = QComboBox()
        self.cb_lc_8164_wave.addItems(['nm'])
        self.hl_lc_8164_wave.addWidget(self.lb_lc_8164_wave)
        self.hl_lc_8164_wave.addWidget(self.le_lc_8164_wave_start)
        self.hl_lc_8164_wave.addWidget(QLabel("~"))
        self.hl_lc_8164_wave.addWidget(self.le_lc_8164_wave_stop)
        self.hl_lc_8164_wave.addWidget(self.cb_lc_8164_wave)
        self.hl_lc_8164_wave.addStretch(1)

        self.wd_lc_7744_range = QWidget()
        self.hl_lc_7744_range = QHBoxLayout(self.wd_lc_7744_range)
        self.hl_lc_7744_range.setContentsMargins(0, 0, 0, 0)
        self.lb_lc_7744 = QLabel("7744C:")
        self.lb_lc_7744_range = QLabel("量程设置：")
        self.cb_lc_7744_range = QComboBox()
        self.cb_lc_7744_range.addItems(["1", "2", "3"])
        self.hl_lc_7744_range.addWidget(self.lb_lc_7744_range)
        self.hl_lc_7744_range.addWidget(self.cb_lc_7744_range)
        self.hl_lc_7744_range.addStretch(1)

        self.gl_lc_settings.addWidget(self.lb_lc_8164, 0, 0)
        self.gl_lc_settings.addWidget(self.wd_lc_8164_power, 0, 1)
        self.gl_lc_settings.addWidget(self.wd_lc_8164_step, 1, 1)
        self.gl_lc_settings.addWidget(self.wd_lc_8164_wave, 2, 1)
        self.gl_lc_settings.addWidget(self.lb_lc_7744, 3, 0)
        self.gl_lc_settings.addWidget(self.wd_lc_7744_range, 3, 1)

        self.le_oo_8164_wave.setFixedWidth(LE_LENGTH)
        self.le_oo_8164_power.setFixedWidth(LE_LENGTH)
        self.le_oe_8164_wave.setFixedWidth(LE_LENGTH)
        self.le_oe_8164_power.setFixedWidth(LE_LENGTH)
        self.le_oe_source_offset.setFixedWidth(LE_LENGTH)
        self.le_lc_8164_step.setFixedWidth(LE_LENGTH)
        self.le_lc_8164_power.setFixedWidth(LE_LENGTH)
        self.le_lc_8164_wave_start.setFixedWidth(70)
        self.le_lc_8164_wave_stop.setFixedWidth(70)
        self.cb_oo_8164_wave.setFixedWidth(UNIT_LENGTH)
        self.cb_oo_7744_range.setFixedWidth(LE_LENGTH)
        self.cb_oo_8164_power.setFixedWidth(UNIT_LENGTH)
        self.cb_oe_8164_wave.setFixedWidth(UNIT_LENGTH)
        self.cb_oe_8164_power.setFixedWidth(UNIT_LENGTH)
        self.cb_oe_source_offset.setFixedWidth(UNIT_LENGTH)
        self.cb_oe_source_speed.setFixedWidth(LE_LENGTH)
        self.cb_oe_source_range.setFixedWidth(LE_LENGTH)
        self.cb_lc_8164_step.setFixedWidth(UNIT_LENGTH)
        self.cb_lc_8164_wave.setFixedWidth(UNIT_LENGTH)
        self.cb_lc_8164_power.setFixedWidth(UNIT_LENGTH)
        self.cb_lc_7744_range.setFixedWidth(LE_LENGTH)

        self.hl_global_settings.addWidget(self.gb_oo_settings)
        self.hl_global_settings.addWidget(self.gb_oe_settings)
        self.hl_global_settings.addWidget(self.gb_lc_settings)

        # 测试区域
        self.gb_test_area = QGroupBox("Test Area")
        self.vl_test_area = QVBoxLayout(self.gb_test_area)
        self.vl_test_area.setContentsMargins(0, 0, 0, 0)
        self.lw_test_area = QListWidget()
        self.lw_test_area.setSpacing(3)
        # self.lw_test_area.setWrapping(True)
        # self.lw_test_area.setFlow(QListView.LeftToRight)
        self.vl_test_area.addWidget(self.lw_test_area)

        # 设置主界面
        self.vl_main.addWidget(self.wd_top)
        self.vl_main.addWidget(self.gb_global_settings)
        self.vl_main.addWidget(self.gb_test_area)
        self.setCentralWidget(self.wd_main)


if __name__ == '__main__':
    app = QApplication(sys.argv)
    window = Gui()
    window.show()
    sys.exit(app.exec())
